<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "Cooperator".
 */
class Cooperator extends ActiveRecord {

    // only define rules for those attributes that
    // will receive user inputs.
    public function rules()
    {
        return [
            [['name', 'link', 'image'], 'required'],
            [['status', 'deleted_at'], 'integer'],
            [['name', 'image'], 'string', 'max' => 128],
            [['link'], 'url'],
        ];
    }
    /**
     * @name 字段名称
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'name' => '合作者',
            'link' => '网址',
            'image' => '图标',
            'status' => '状态',
            'deleted_at' => '删除时间',
        ];
    }
    /**
     * @name update & insert data check config for html
     * @param $type string 页面操作类型
     * @param $encodeJson boolean 是否转成json串
     * @return string / array
     */
    public static function flyer($type = 'update')
    {
        // jsut search
        $rule = [
            'param' => [
                'name' => ['网站名称', ['maxlength' => 128, 'required']],
                'link' => ['网址', ['url', 'required']],
                'image' => ['图标', ['maxlength' => 128, 'required']],
            ],
        ];
        return $rule;
    }

    /**
     * @name 上传新图片时删除老图片
     * @param $image string 老图片路径
     * @return bool
     */
    public function unlinkImage($image)
    {
        if(empty($image) || ($image == $this->image)) {
            return true;
        }
        @unlink(Yii::getAlias('@upload/'.$image));
        return true;
    }
}